<?php
namespace Api\Controller;

class PayController extends ApiController
{

    /**
     * 支付方式
     */
    public function payments()
    {
        $M_payment = M('payment');
        $list = $M_payment->field('code,title')->select();
        $list ? response(1, $list) : response(- 2);
    }

    /**
     * 支付
     *
     * @param
     *            int order_id
     * @param
     *            string order_no
     * @param
     *            double price
     * @param
     *            string paycode
     */
    public function pay()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array(
            'price',
            'paycode'
        ));
        
        $M_order = M('order');
        $M_payment_log = M('payment_log');
        $M_checkup = M('checkup');
        
        $paycode = I('post.paycode');
        $price = I('post.price');
        $order_no = I('post.order_no');
        $checkup_no = I('post.checkup_no');
        $order_id = I('post.order_id');
        $checkup_id = I('post.checkup_id');
        
        $price = I('post.price');
        ! is_numeric($price) && response(- 1, '', '金额错误');
        (int) $price < 0 && response(- 1, '', '金额错误');
        
        if (empty($checkup_no) && empty($order_no)) {
            response(- 1, '', '订单号或鉴定单号不存在');
        }
        
        $payment_log_data = array();
      
        $payment_log_data['uid'] = API_UID;
        $payment_log_data['create_time'] = NOW_TIME;
        $payment_log_data['method'] = '+';
        $payment_log_data['payment_code'] = $paycode;
        $payment_log_data['status'] = 1;
        $payment_log_data['price'] = $price;
        $payment_log_data['transaction_no'] = rand(10000000, 99999999);
        
        $bill_data = array();
        $bill_data['uid'] = API_UID;
        $bill_data['create_time'] = NOW_TIME;
        $bill_data['money'] = $price;
        $bill_data['sign'] = '-';
        $bill_data['type'] = 2;
        $bill_data['status'] = 1;
        
        // 订单支付
        if ($order_id) {
            /* 检查订单状态 */
            is_array($order_id) && $order_id = implode(',', $order_id);
            is_array($order_no) && $order_no = implode(',', $order_no);
            
            $val_map = array();
            $val_map['id'] = array(
                'in',
                $order_id
            );
            $val_map['status'] = array(
                'EGT',
                1
            );
            $M_order->where($val_map)->find() && response(- 1, '', '订单中出现重复支付,请重试');
            
            $payment_log_data['order_id'] = $order_id;
            $payment_log_data['order_no'] = $order_no;
            $payment_log_data['log'] = '订单：' . $order_no . '支付';
            
            $bill_data['title'] = '订单支付' . $order_no;
            $bill_data['desc'] = '订单号：' . $order_no . ' 金额:' . $price;
            
            $map = array();
            $map['id'] = array(
                'in',
                $order_id
            );
            
            if (is_array($order_id) || is_array($order_no)) {
                // count($order_id) != count($order_no) &&
                // response(-1,'','订单与价格匹配错误');
            }
            
            // $sum=$M_order->where($map)->Sum('price');
            
            // (double)$sum !=(double)$price && response(-1,'','订单价格不一致');
            $payment_log_id = $M_payment_log->add($payment_log_data);
            if ($payment_log_id) {
                M('bill')->add($bill_data);
                $result = $M_order->where($map)->save(array(
                    'pay_time'=>NOW_TIME,
                    'status' => 1,
                    'payment_log_id' => $payment_log_id
                ));
                
                addMessage(2,'付款通知','您的订单'.$order_no.'支付成功',API_UID);
                response(1, '', '支付成功');
            } else {
                response(- 1, '', '支付遇到问题');
            }
        }         /* 我的鉴定支付 */
        else {
            // 检查鉴定状态
            is_array($checkup_id) && $checkup_id = implode(',', $checkup_id);
            is_array($checkup_no) && $checkup_no = implode(',', $checkup_no);
            
            $val_map = array();
            $val_map['id'] = array(
                'in',
                $checkup_id
            );
            $val_map['status'] = array(
                'EGT',
                1
            );
            $M_checkup->where($val_map)->find() && response(- 1, '', '鉴定单中出现重复支付,请重试');
            
            $payment_log_data['checkup_id'] = $checkup_id;
            $payment_log_data['checkup_no'] = $checkup_no;
            $payment_log_data['log'] = '鉴定单号：' . $checkup_no . '支付';
            
            $bill_data['title'] = '鉴定支付' . $checkup_no;
            $bill_data['desc'] = '鉴定单号：' . $checkup_no . ' 金额:' . $price;
            
            $map = array();
            $map['id'] = array(
                'in',
                $checkup_id
            );
            
            if (is_array($checkup_id) || is_array($checkup_no)) {
                // count($checkup_id) != count($checkup_no) &&
                // response(-1,'','鉴定单与价格匹配错误');
            }
            
            // $sum=$M_checkup->where($map)->Sum('price');
            
            // (double)$sum !=(double)$price && response(-1,'','鉴定单价格不一致');
            $payment_log_id = $M_payment_log->add($payment_log_data);
            if ($payment_log_id) {
                M('bill')->add($bill_data);
                $map = array();
                $map['id'] = array(
                    'in',
                    $checkup_id
                );
                
                $M_checkup->where($map)->save(array(
                    'pay_time'=>NOW_TIME,
                    'status' => 1
                ));
                addMessage(2,'付款通知','您的订单'.$checkup_no.'支付成功',API_UID);
                response(1, '', '支付成功');
            } else {
                response(- 1, '', '支付遇到问题');
            }
        }
    }

    /**
     * 余额支付
     *
     * @param
     *            int order_id
     * @param
     *            string order_no
     * @param
     *            double price
     */
    public function balancePay()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        
        params_exist(array(
            'price'
        ));
        
       
        $order_id = I('post.order_id');
        $order_no = I('post.order_no');
        $money = I('post.price');
        $checkup_no = I('post.checkup_no');
        $checkup_id = I('post.checkup_id');
        
        $price = I('post.price');
        ! is_numeric($price) && response(- 1, '', '金额错误');
        (int) $price < 0 && response(- 1, '', '金额错误');
        
        if (empty($checkup_no) && empty($order_no)) {
            response(- 1, '', '订单号或鉴定单号不存在');
        }
        
        $M_order = M('order');
        $M_payment_log = M('payment_log');
        $M_member = M('member');
        $M_checkup = M('checkup');
        
        $member_info = $M_member->where(array(
            'uid' => API_UID
        ))->find();
        
        $payment_log_data = array();
        $paycode='balancepay';
        $payment_log_data['uid'] = API_UID;
        $payment_log_data['create_time'] = NOW_TIME;
        $payment_log_data['method'] = '+';
        $payment_log_data['payment_code'] = $paycode;
        $payment_log_data['status'] = 1;
        $payment_log_data['price'] = $price;
        $payment_log_data['transaction_no'] = rand(10000000, 99999999);
        
        
        $bill_data = array();
        $bill_data['uid'] = API_UID;
        $bill_data['create_time'] = NOW_TIME;
        $bill_data['sign'] = '-';
        $bill_data['type'] = 2;
        $bill_data['money'] = $money;
        $bill_data['status'] = 1;
        
        /* 订单支付 */
        if ($order_id) {
            // 检查订单状态
            is_array($order_id) && $order_id = implode(',', $order_id);
            is_array($order_no) && $order_no = implode(',', $order_no);
            
            $val_map = array();
            $val_map['id'] = array(
                'in',
                $order_id
            );
            $val_map['status'] = array(
                'EGT',
                1
            );
            $M_order->where($val_map)->find() && response(- 1, '', '订单中出现重复支付,请重试');
            
            (double) $money > (double) $member_info['amount'] && response(- 1, '', '余额不足');
            $result = $M_member->where(array(
                'uid' => API_UID
            ))->setDec('amount', $money);
            
            if ($result) {
                
                $payment_log_data['order_id'] = $order_id;
                $payment_log_data['order_no'] = $order_no;
                $payment_log_data['log'] = '订单：' . $order_no . ' 支付'. ' 支付方式:'.$paycode;
    
                $bill_data['title'] = '订单支付' . $order_no;
                $bill_data['desc'] = '订单号：' . $order_no . ' 金额:' . $price . ' 支付方式:'.$paycode ;
                
                $payment_log_id = $M_payment_log->add($payment_log_data);
                M('bill')->add($bill_data);
                
                $map = array();
                $map['id'] = array(
                    'in',
                    $order_id
                );
                
                $M_order->where($map)->save(array(
                    'pay_time'=>NOW_TIME,
                    'status' => 1,
                    'payment_log_id' => $payment_log_id
                ));
                addMessage(2,'订单付款','您的订单'.$checkup_no.'支付成功',API_UID);
                response(1, '', '支付成功');
            } else {
                response(- 9);
            }
        }         /* 我的鉴定支付 */
        else {
            // 检查鉴定状态
            is_array($checkup_id) && $checkup_id = implode(',', $checkup_id);
            is_array($checkup_no) && $checkup_no = implode(',', $checkup_no);
            
            $val_map = array();
            $val_map['id'] = array(
                'in',
                $checkup_id
            );
            $val_map['status'] = array(
                'EGT',
                1
            );
            $M_checkup->where($val_map)->find() && response(- 1, '', '鉴定单中出现重复支付,请重试');
            
            (double) $money > (double) $member_info['amount'] && response(- 1, '', '余额不足');
            $result = $M_member->where(array(
                'uid' => API_UID
            ))->setDec('amount', $money);
            
            if ($result) {
                
               $payment_log_data['checkup_id'] = $checkup_id;
                $payment_log_data['checkup_no'] = $checkup_no;
                $payment_log_data['log'] = '鉴定单号：' . $checkup_no . '支付' . ' 支付方式:'.$paycode;
                
                $bill_data['title'] = '鉴定支付' . $checkup_no;
                $bill_data['desc'] = '鉴定单号：' . $checkup_no . ' 金额:' . $price . ' 支付方式:'.$paycode;                
    
                $payment_log_id = $M_payment_log->add($payment_log_data);
                M('bill')->add($bill_data);
                
                $map = array();
                $map['id'] = array(
                    'in',
                    $checkup_id
                );
                
                $M_checkup->where($map)->save(array(
                    'pay_time'=>NOW_TIME,
                    'status' => 1,
                    'payment_log_id' => $payment_log_id
                ));
                addMessage(2,'鉴定付款','您的鉴定单'.$checkup_no.'支付成功',API_UID);
                response(1, '', '支付成功');
            } else {
                response(- 9);
            }
        }
    }

    /**
     * 充值
     * 
     * @param
     *            double money
     * @param
     *            string paycode
     */
    public function recharge()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array(
            'money',
            'paycode'
        ));
        
        $money = I('post.money');
        $paycode = I('post.paycode');
        
        $M_member = M('member');
        $M_order = M('order');
        $M_payment_log = M('payment_log');
        
        ! is_numeric($money) && response(- 1, '', '充值金额错误');
        (int) $money < 0 && response(- 1, '', '充值金额错误');
        
        $payment_log_data = array();
        $payment_log_data['uid'] = API_UID;
        $payment_log_data['create_time'] = NOW_TIME;
        $payment_log_data['method'] = '+';
        $payment_log_data['payment_code'] = $paycode;
        $payment_log_data['status'] = 1;
        $payment_log_data['price'] = $money;
        $payment_log_data['log'] = '充值';
        
        $payment_log_id = $M_payment_log->add($payment_log_data);
        
        $bill_data = array();
        $bill_data['uid'] = API_UID;
        $bill_data['create_time'] = NOW_TIME;
        $bill_data['title'] = '充值:金额' . $money;
        $bill_data['desc'] = '充值:金额' . $money;
        $bill_data['sign'] = '+';
        $bill_data['type'] = 1;
        $bill_data['money'] = $money;
        $bill_data['status'] = 1;
        
        M('bill')->add($bill_data);
        
        if ($payment_log_id) {
            $result = $M_member->where(array(
                'uid' => API_UID
            ))->setInc('amount', $money);
            
            response(1, '', '充值成功');
        } else {
            response(- 1, '', '充值失败');
        }
    }
}
?>